
if (typeof(SWFUpload) === "function") {

	SWFUpload.path = '';
	(function(){
		var dom = jQuery('script');
		for (var i = 0; i < dom.length; i++) {
			j = '' + jQuery(dom[i]).attr('src');
			j = j.toLowerCase();
			j = j.split('jquery.swfupload.js');
			if (j.length == 2) {
				SWFUpload.path = j[0];
			}
		}
	})();

	if (SWFUpload.queue === undefined) {
		SWFUpload.queue = {};
	}

	SWFUpload.prototype.initSettings = (function (oldInitSettings) {
		return function (userSettings) {
			if (typeof(oldInitSettings) === "function") {
				oldInitSettings.call(this, userSettings);
			}

			this.settings.file_upload_limit = this.settings.file_upload_limit == 0 ? 500 : this.settings.file_upload_limit;
			this.settings.file_queue_limit = this.settings.file_queue_limit == 0 ? 10 : this.settings.file_queue_limit;
			this.settings.file_queue_limit = Math.min(this.settings.file_upload_limit, this.settings.file_queue_limit);

			this.settings.button_window_mode = SWFUpload.WINDOW_MODE.TRANSPARENT;
			this.settings.button_cursor = SWFUpload.CURSOR.HAND;

			if (/^swfupload.swf/.exec(this.settings.flash_url) !== null) {
				this.settings.flash_url = SWFUpload.path + this.settings.flash_url;
			}

			if (/^swfupload_fp9.swf/.exec(this.settings.flash9_url) !== null) {
				this.settings.flash9_url = SWFUpload.path + this.settings.flash9_url;
			}

			if (this.settings.swfupload_load_failed_handler === null) {
				this.settings.swfupload_load_failed_handler = function() {
					alert('Flash Player未安装或者它的版本低于 9.0.28');
				}
			}

			if (this.settings.button_text == '') {
				this.settings.button_text = 'Upload';
			}

			if (this.settings.button_width == 1) {
				this.settings.button_width = 50;
			}

			if (this.settings.button_height == 1) {
				this.settings.button_height = 20;
			}

			if (this.queueSettings === undefined) {
				this.queueSettings = {};
			}

			this.queueSettings.user_file_queue_error_handler = this.settings.file_queue_error_handler;
			this.settings.file_queue_error_handler = SWFUpload.queue.fileQueueErrorHandler;

			this.queueSettings.user_upload_error_handler = this.settings.upload_error_handler;
			this.settings.upload_error_handler = SWFUpload.queue.uploadErrorHandler;

		};
	})(SWFUpload.prototype.initSettings);

	SWFUpload.queue.fileQueueErrorHandler = function (file, code, message) {
		if (typeof(this.queueSettings.user_file_queue_error_handler) === "function") {
			switch (code) {
				case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
					message = '选取的文件数量超过限制！最多只能选择' + this.settings.file_queue_limit + '个文件！';
					break;
				case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
					message = '文件（' + file.name + '）的大小超过最大上传大小限制！';
					break;
				case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
					message = '文件（' + file.name + '）是空文件！';
					break;
				case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
					message = '文件（' + file.name + '）是不允许的上传文件类型！';
					break;
			}
			this.queueSettings.user_file_queue_error_handler.call(this, file, code, message);
		}
	}

	SWFUpload.queue.uploadErrorHandler = function (file, code, message) {
		if (typeof(this.queueSettings.user_upload_error_handler) === "function") {
			switch (code) {
				case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
					message = 'HTTP错误！';
					break;
				case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
					message = '上传必须设置！';
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
					message = '尝试初始化上传时出现了错误！';
					break;
				case SWFUpload.UPLOAD_ERROR.IO_ERROR:
					message = '读取或传输文件时发生错误！';
					break;
				case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
					message = '安全错误！';
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
					message = '上传的文件数量超过允许的最大值！';
					break;
				case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
					message = '上传文件未找到！';
					break;
				case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
					message = '上传验证失败！';
					break;
				case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
					message = '上传被取消！';
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
					message = '上传被暂停！';
					break;
				case SWFUpload.UPLOAD_ERROR.RESIZE_ERROR:
					message = '调整图像大小时出现了某错误！';
					break;
				default:
					message = '未知错误！';
					break;
			}
			this.queueSettings.user_upload_error_handler.call(this, file, code, message);
		}
	}

}